package cn.kgc.service.impl;

import cn.kgc.client.*;
import cn.kgc.dto.Dto;
import cn.kgc.dto.DtoUtil;
import cn.kgc.exception.ItemErrorCode;
import cn.kgc.pojo.*;
import cn.kgc.service.ItemService;
import cn.kgc.utils.BaseException;
import cn.kgc.utils.Constants;
import cn.kgc.utils.DateUtil;
import cn.kgc.vo.ItemDetailVo;
import cn.kgc.vo.ItemPriceVo;
import cn.kgc.vo.ItemSchedulerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; 
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    RestDmItemClient restDmItemClient;

    @Autowired
    RestDmImageClient restDmImageClient;

    @Autowired
    RestDmCinemaClient restDmCinemaClient;

    @Autowired
    RestDmSchedulerClient restDmSchedulerClient;


    @Autowired
    RestDmSchedulerSeatPriceClient restDmSchedulerSeatPriceClient;

    @Autowired
    RestDmSchedulerSeatClient restDmSchedulerSeatClient;


    @Override
    public Dto<ItemDetailVo> quaryItemDetail(Integer id) throws Exception {

        //1.获取商品数据，id获取商品的信息
        DmItem dmItemById = restDmItemClient.getDmItemById(new Long(id));

        //2.获取商品对应的图片
        DmImage dmImage = restDmImageClient.getDmImage_DefaultImage(dmItemById.getId(),
                Constants.Image.ImageType.poster,
                Constants.Image.ImageCategory.item).get(0);



        //3.获取商品的影院信息
        DmCinema dmCinemaById = restDmCinemaClient.getDmCinemaById(dmItemById.getCinemaId());
        //4.组装数据Vo中
        ItemDetailVo itemDetailVo=copyDatatoVo(dmItemById,dmImage,dmCinemaById);
        return DtoUtil.returnDataSuccess(itemDetailVo);
    }

    @Override
    public Dto<ItemSchedulerVo> queryItemScheduler(Integer itemId) throws Exception {
        //1.查询节目id是否存在
        DmItem dmItemById = restDmItemClient.getDmItemById(new Long(itemId));
        ArrayList<ItemSchedulerVo> itemSchedulerVos = new ArrayList<>();
        if(dmItemById==null){
            //没有这个节目
            //传进来的id是错误的节目
            //返回错误信息给前端
            throw  new BaseException(ItemErrorCode.ITEM_No_EXIST);

        }
        //2.查看根据商品Id查看这个商品对应的排期信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("itemId",itemId);
        List<DmScheduler> dmSchedulerListByMap = restDmSchedulerClient.getDmSchedulerListByMap(map);
        if(dmSchedulerListByMap==null){
            throw new BaseException(ItemErrorCode.ITEM_NO_Scheduler);
        }

        //3.组装Vo返回
        for (DmScheduler dmScheduler : dmSchedulerListByMap) {
            ItemSchedulerVo itemSchedulerVo = new ItemSchedulerVo();
            //导数据
            BeanUtils.copyProperties(dmScheduler,itemSchedulerVo);
            itemSchedulerVo.setStartTime(DateUtil.format(dmScheduler.getStartTime()));
            itemSchedulerVo.setEndTime(DateUtil.format(dmScheduler.getEndTime()));

            itemSchedulerVos.add(itemSchedulerVo);
        }
        return DtoUtil.returnDataSuccess(itemSchedulerVos);
    }

    @Override
    public Dto<List<ItemPriceVo>> queryItemPrice(Integer schedulerId) throws Exception {
        //1.判断schedulerId是否存在
        //2.查询Id对应的排期
        DmScheduler dmSchedulerById = restDmSchedulerClient.getDmSchedulerById(new Long(schedulerId));
        if(dmSchedulerById==null){
            throw  new BaseException(ItemErrorCode.ITEM_NO_Scheduler);
        }
        //3.查询座位价格
        java.util.Map map =new HashMap<String,Object>();
        map.put("scheduleId",schedulerId);
        List<DmSchedulerSeatPrice> dmSchedulerSeatPriceList= restDmSchedulerSeatPriceClient.getDmSchedulerSeatPriceListByMap(map);

        if(dmSchedulerSeatPriceList==null){
            throw new BaseException(ItemErrorCode.Item_No_SeatPrice);
        }
        //4.查询是否有座位
         java.util.List  list =new ArrayList<ItemPriceVo>();
        for (DmSchedulerSeatPrice dmSchedulerSeatPrice : dmSchedulerSeatPriceList) {
            ItemPriceVo itemPriceVo = new ItemPriceVo();
            Integer areaLevel = dmSchedulerSeatPrice.getAreaLevel();
            java.util.Map seatMap= new HashMap<String,Object>();
            seatMap.put("areaLevel",areaLevel);
            seatMap.put("schedulerId",schedulerId);
            seatMap.put("status",1);
            Integer dmSchedulerCountByMap = restDmSchedulerClient.getDmSchedulerCountByMap(seatMap);
            if(dmSchedulerCountByMap>0){
                itemPriceVo.setIsHaveSeat(Constants.SchedulerSeatStatus.SchedulerSeat_FREE);
            }else{ itemPriceVo.setIsHaveSeat(Constants.SchedulerSeatStatus.SchedulerSeat_No_Seat);
            }
            BeanUtils.copyProperties(dmSchedulerSeatPrice,itemPriceVo);
            list.add(itemPriceVo);
        }
        //5.组装数据返回
        return DtoUtil.returnDataSuccess(list);
    }


    //将3个对象信息封装到Vo中
    private ItemDetailVo copyDatatoVo(DmItem dmItemById, DmImage dmImage, DmCinema dmCinemaById) throws ParseException {
        ItemDetailVo itemDetailVo=new ItemDetailVo();
        //商品数据转移,类型，名字一致，才能赋值
        BeanUtils.copyProperties(dmItemById,itemDetailVo);
        //对个别数据类型不一致的属性赋值
        itemDetailVo.setStartTime(DateUtil.format(dmItemById.getStartTime()));
        itemDetailVo.setEndTime(DateUtil.format(dmItemById.getEndTime()));
        itemDetailVo.setState(dmItemById.getState()+"");

        //影院                                               忽略掉Id属性，不拷贝
        BeanUtils.copyProperties(dmCinemaById,itemDetailVo,"id");

        //图片数据
        itemDetailVo.setImgUrl(dmImage.getImgUrl());

        return itemDetailVo;
    }
}
